﻿using System;
using System.Collections.Generic;
using System.Linq;
using HDSmartCommunity.BUS;
using HDSmartCommunity.DAL;
using HDSmartCommunity.DevicePlatform;
using HDSmartCommunity.Models;
using HDSmartCommunity.Serial;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace HDSmartCommunity.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class RequestController : ControllerBase
    {
        public ComPassDeviceSerialData compass_serial = SerialServer.compass_serial;
        /// <summary>
        ///删除用户
        /// </summary>
        /// <returns>success</returns>
        [HttpGet]
        [HttpPost]
        [Route("delPerson")]
        public delPersonReturn delPerson(PersonDeleleInfo personDeleleInfo)
        {
            LogHelper.Log(JsonConvert.SerializeObject(personDeleleInfo));
            delPersonReturn delPersonReturn = new delPersonReturn();
            delPersonReturn.method = "delPerson";
            delPersonReturn.req_id = personDeleleInfo.req_id;
            try
            {

                CardInfoDAL cardInfoDAL = new CardInfoDAL();

                bool result = cardInfoDAL.DeletePerson(personDeleleInfo.data[0].PersonID);
                if (!result)
                {
                    delPersonReturn.code = 4;
                    delPersonReturn.msg = "同步失败";
                    return delPersonReturn;
                }
                delPersonReturn.code = 0;
                delPersonReturn.msg = "success";
            }
            catch (Exception ex)
            {
                delPersonReturn.code = 8;
                delPersonReturn.msg = "同步异常";
            }
            LogHelper.Log(JsonConvert.SerializeObject(delPersonReturn));
            return delPersonReturn;
        }

        /// <summary>
        ///清空用户
        /// </summary>
        /// <returns>success</returns>
        [HttpGet]
        [HttpPost]
        [Route("clearPerson")]
        public delPersonReturn clearPerson(PersonClearInfo personDeleleInfo)
        {
            LogHelper.Log(JsonConvert.SerializeObject(personDeleleInfo));
            delPersonReturn delPersonReturn = new delPersonReturn();
            delPersonReturn.method = "clearPerson";
            delPersonReturn.req_id = personDeleleInfo.req_id;
            try
            {

                CardInfoDAL cardInfoDAL = new CardInfoDAL();
                bool result = cardInfoDAL.ClearPerson();
                if (result)
                {
                    delPersonReturn.code = 0;
                    delPersonReturn.msg = "success";
                }
                else
                {
                    delPersonReturn.code = 1;
                    delPersonReturn.msg = "清空异常";
                }

            }
            catch (Exception ex)
            {
                delPersonReturn.code = 8;
                delPersonReturn.msg = "同步异常";
            }
            LogHelper.Log(JsonConvert.SerializeObject(delPersonReturn));

            return delPersonReturn;
        }

        /// <summary>
        ///同步用户
        /// </summary>
        /// <returns>success</returns>
        [HttpGet]
        [HttpPost]
        [Route("savePerson")]
        public PersonReturn savePerson(PersonSnyc personSnycInfo)
        {
            LogHelper.Log(JsonConvert.SerializeObject(personSnycInfo));
            PersonReturn personReturn = new PersonReturn();
            personReturn.method = "savePerson";
            try
            {
                CardInfoDAL cardInfoDAL = new CardInfoDAL();
                string[] arry = personSnycInfo.cardNo.Split(',');
                if (personSnycInfo.frontFloor == null)
                {
                    personSnycInfo.frontFloor = "";
                }
                if (personSnycInfo.backFloor == null)
                {
                    personSnycInfo.backFloor = "";
                }
                for (int i = 0; i < arry.Length; i++)
                {
                    if (personSnycInfo.stateID == 6 && !string.IsNullOrEmpty(arry[i]))
                    {
                        personSnycInfo.cardNo = arry[i];

                        List<OwnerInfo> list = cardInfoDAL.QueryOwner(personSnycInfo.personID, arry[i]);
                        bool result = false;
                        if (list != null && list.Count > 0)
                        {
                            result = cardInfoDAL.UpdatePerson(personSnycInfo);
                        }
                        else
                        {
                            result = cardInfoDAL.InsertPerson(personSnycInfo);
                        }

                        if (result)
                        {
                            personReturn.code = 0;
                            personReturn.msg = "success";
                        }
                        else
                        {
                            personReturn.code = 4;
                            personReturn.msg = "用户同步异常";
                            return personReturn;
                        }
                    }
                    else
                    {
                        bool result = cardInfoDAL.DeletePerson(personSnycInfo.personID);
                        if (!result)
                        {
                            personReturn.code = 4;
                            personReturn.msg = "同步失败";
                            return personReturn;
                        }
                        personReturn.code = 0;
                        personReturn.msg = "success";
                    }
                }
            }
            catch (Exception ex)
            {
                personReturn.code = 8;
                personReturn.msg = "删除异常";
            }

            return personReturn;
        }

        /// <summary>
        ///梯控模式
        /// </summary>
        /// <returns>success</returns>
        [HttpGet]
        [HttpPost]
        [Route("setLiftMode")]
        public LiftModeReturn setLiftMode(LiftMode liftMode)
        {
            LogHelper.Log(JsonConvert.SerializeObject(liftMode));
            LiftModeReturn liftModeReturn = new LiftModeReturn();
            liftModeReturn.method = "setLiftMode";
            liftModeReturn.req_id = liftMode.req_id;
            try
            {
                CardInfoDAL cardInfoDAL = new CardInfoDAL();

                bool result = cardInfoDAL.UpdateLiftMode(liftMode.data.liftmode);
                if (result)
                {
                    compass_serial.workmode = liftMode.data.liftmode;
                }
                else
                {
                }

                liftModeReturn.code = 0;
                liftModeReturn.msg = "success";
            }
            catch (Exception ex)
            {
                liftModeReturn.code = 8;
                liftModeReturn.msg = "删除异常";
            }
            LogHelper.Log(JsonConvert.SerializeObject(liftModeReturn));

            return liftModeReturn;
        }
    }
}
